package demo.view.player
{
	import asset.testRenAsset;
	
	import demo.events.DemoPlayerEvent;
	import demo.vo.PlayerVO;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import map.MapAStar;
	import map.MapPoint3D;
	import map.MapSprite;
	import map.MapUtils;

	public class DemoPlayerBase extends MapSprite
	{
		private var _playerVO:PlayerVO;//玩家实体数据
		private var _mapAStar:MapAStar;//A星寻路类
		private var _roadArray:Array;//地图路点
		private var _targetPoint:MapPoint3D;//行动目的点
		private var _mapCellSize:Number;//地图单元格大小
		private var _mapPathArray:Array;//行动路径
		private var _actionTimer:Timer;//用与寻路行动
		private var _mapMoveNode:Array;//当前移动路径节点
		
		/**
		 * 玩家基类 
		 * @param playerVO 玩家实体数据
		 * @param roadArray 地图路点
		 * @param mapCellSize 地图单元格大小
		 */		
		public function DemoPlayerBase(playerVO:PlayerVO, roadArray:Array, mapCellSize:Number)
		{
			_playerVO=playerVO;
			_roadArray=roadArray;
			_mapCellSize=mapCellSize;
			
			setView();
			setEvent();
		}
		
		private function setView():void
		{
			var bm:Bitmap=new Bitmap(new testRenAsset());
			bm.x=-bm.width/2;
			bm.y=-bm.height;
			addChild(bm);
			
			_mapAStar=new MapAStar(_roadArray);
			_actionTimer=new Timer(1000/25);
		}
		
		private function setEvent():void
		{
			//帧听玩家行动目标点是否改变
			_playerVO.addEventListener(DemoPlayerEvent.PLAYER_TARGET_POINT_CHANGE, __onPlayerTargetPointChange);
			_actionTimer.addEventListener(TimerEvent.TIMER, __onPlayerMove);
		}
		
		private function removeEvent():void
		{
			_playerVO.removeEventListener(DemoPlayerEvent.PLAYER_TARGET_POINT_CHANGE, __onPlayerTargetPointChange);
			_actionTimer.removeEventListener(TimerEvent.TIMER, __onPlayerMove);
		}
		
		/**
		 * 当玩家行动目标点改变
		 */		
		private function __onPlayerTargetPointChange(evt:DemoPlayerEvent):void
		{
			_targetPoint=MapUtils.screenToSpace(_playerVO.targetPoint);//转换行动目标点为空间坐标
			var targetGridPoint:MapPoint3D=MapUtils.getGridPoint(_targetPoint, _mapCellSize);//目标坐标进行地图单元格转换
			var boxGridPoint:MapPoint3D = MapUtils.getGridPoint(position, _mapCellSize);//玩家当前坐标进行地图单元格转换
			
			//开始寻路
			_mapPathArray = _mapAStar.find(boxGridPoint.x, boxGridPoint.z, targetGridPoint.x, targetGridPoint.z);
			if (_mapPathArray != null && _mapPathArray.length > 0)
			{
				_mapPathArray.shift();
				_mapMoveNode=_mapPathArray[0] as Array;
				_actionTimer.start();
			}
		}
		
		/**
		 *玩家行动 
		 */		
		private function __onPlayerMove(evt:TimerEvent):void
		{
			if (_mapMoveNode && _mapPathArray)
			{
				position=new MapPoint3D(_mapMoveNode[0] * _mapCellSize+(_mapCellSize/4)*3, 0, _mapMoveNode[1] * _mapCellSize+(_mapCellSize/4)*3);
				_mapMoveNode = _mapPathArray.shift() as Array;
				dispatchEvent(new DemoPlayerEvent(DemoPlayerEvent.PLAYER_POINT_CHANGE));
			}
			else
			{
				_actionTimer.stop();
				dispatchEvent(new DemoPlayerEvent(DemoPlayerEvent.PLAYER_GOTO_TARGET_POINT));
			}
		}
		
		public function get playerVO():PlayerVO
		{
			return _playerVO;
		}

		public function set playerVO(value:PlayerVO):void
		{
			_playerVO = value;
		}

		override public function dispose():void
		{
			super.dispose();
			removeEvent();
			_playerVO=null;
			_mapAStar=null;
			_roadArray=null;
			_targetPoint=null;
			_mapPathArray=null;
			_actionTimer.stop();
			_actionTimer=null;
			_mapMoveNode=null;
			
			if(parent) parent.removeChild(this);
		}
	}
}